const app = getApp();
const globalData = getApp().globalData;
const regeneratorRuntime = globalData.regeneratorRuntime;
var util = require('../../../utils/util.js');
const screenService = require("../../../apiservice/screen/screenservice")

const buttons = [{
		openType: 'new',
		label: '新建',
		icon: "../../../images/pages/new.png",
	},
	{
		openType: 'account',
		label: '客户',
		icon: "../../../images/pages/search.png",
	},
	{
		openType: 'stat',
		label: '统计',
		icon: "../../../images/pages/stat.png",
	}
];

Page({
	data: {
		buttons,
		screenList: [],
		pagenum: 0,
		displaytotal: 0,
		isnextPage: false,
		total: 0,
		height: 0,
		activeIndex:0,
	},

	GetAllPage: function(pageNum, pageSize) {
		var that = this;
		screenService.getScreenAllPage(pageNum, pageSize).then(function(result) {
			//debugger;
			if (result.data && result.data && result.data.datas) {
				var displaytotal = that.data.displaytotal + result.data.datas.length;
				that.setData({
					['screenList[' + (pageNum - 1) + ']']: result.data.datas
				});
				that.setData({
					pagenum: pageNum,
					displaytotal: displaytotal,
					isnextPage: result.data.existsnextdata,
					total: result.data.total
				})
				console.log(result.data);
			} else {
				that.setData({
					pagenum: 0,
					displaytotal: 0,
					isnextPage: false,
					total: 0,
					['screenList']: null
				});
			}
			wx.hideLoading();
		}).catch(function(error) {
			console.log(error);
			wx.hideLoading();
		});

	},

	GetStatePage: function(pageNum, pageSize, state) {
		var that = this;
		screenService.getScreenStatusPage(pageNum, pageSize, state).then(function(result) {
			//debugger;
			if (result.data && result.data && result.data.datas) {
				var displaytotal = that.data.displaytotal + result.data.datas.length;
				that.setData({
					['screenList[' + (pageNum - 1) + ']']: result.data.datas
				});
				that.setData({
					pagenum: pageNum,
					displaytotal: displaytotal,
					isnextPage: result.data.existsnextdata,
					total: result.data.total
				})
				console.log(result.data);
			} else {

			}
			wx.hideLoading();
		}).catch(function(error) {
			console.log(error);
			wx.hideLoading();
		});
	},

	lower: function(e) {
		if (this.data.isnextPage) {
			wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
				title: '加载中',
				icon: 'loading'
			});
			var pageNum = this.data.pagenum;
			var activeIndex = this.data.activeIndex;
			this.switchStateScreen(activeIndex, ++pageNum);
			//this.GetAllPage(++pageNum, 20);
		} else {
			wx.showToast({ //如果全部加载完成了也弹一个框
				title: '我也是有底线的',
				icon: 'success',
				duration: 350
			});
			return false;
		}
	},

	onLoad: function() {
		var res = wx.getSystemInfoSync();
		//debugger;
		this.setData({
			height: res.windowHeight
		});
	},

	onShow: function() {
		if (typeof this.getTabBar === 'function' &&
			this.getTabBar()) {
			this.getTabBar().setData({
				selected: "screen"
			})
		}
		this.setData({
			pagenum: 0,
			displaytotal: 0,
			isnextPage: false,
			total: 0,
			['screenList']: null
		});
		//期间为了显示效果可以添加一个过度的弹出框提示“加载中”
		wx.showLoading({ 
			title: '加载中',
			icon: 'loading'
		});
	

		let userInfo = globalData.userInfo;
		let mobilerole = userInfo.mobilerole_systemuser;
		
    // if (userInfo.site == "爱尔眼科医院集团") { //集团
    //   let roleIds = [1, 2, 3];
    //   this.showTabBarItem(mobilerole, roleIds, "showScreens");

    //   roleIds = [5, 4, 7];
    //   this.showTabBarItem(mobilerole, roleIds, "showPromotion");
    // }
    // else {  //医院  医院配了筛查，则学术推广也显示
    //   let roleIds = [1, 2, 3];  //筛查
    //   if (util.haveRole(mobilerole, roleIds)) {
    //     if (typeof this.getTabBar === 'function' &&
    //       this.getTabBar()) {
    //       this.getTabBar().setData({
    //         ["showScreens"]: true,
    //         ["showPromotion"]: true
    //       })
    //     }
    //   }
    //   else { //医院没有配置筛查
    //     this.getTabBar().setData({
    //       ["showScreens"]: false
    //     })
    //     roleIds = [5, 4, 7];
    //     this.showTabBarItem(mobilerole, roleIds, "showPromotion");
    //   }
    // }

    // let roleIds = [12, 13];
    // this.showTabBarItem(mobilerole, roleIds, "showFollow");
    let roleIds = [1, 2, 3];
    this.showTabBarItem(mobilerole, roleIds, "showScreens");
    roleIds = [5, 4, 7, 13]; //13: 村医
    this.showTabBarItem(mobilerole, roleIds, "showPromotion");
    roleIds = [12];
    this.showTabBarItem(mobilerole, roleIds, "showFollow");

		this.onTabItemTap();

		// Do something when page ready.
		this.switchStateScreenCallBack(1);
	},

	tabClick: function(e) {
		this.setData({
			sliderOffset: e.currentTarget.offsetLeft,
			activeIndex: e.currentTarget.id
		});

		console.log(this.data.activeIndex);
	},

	onsearchAccount: function(e) {
		console.log('onsearchAccount', e)
	},

	onscreenStat: function(e) {
		console.log('onscreenStat', e)
	},

	onChange: function(e) {
		wx.showLoading({ //期间为了显示效果可以添加一个过度的弹出框提示“加载中”  
			title: '加载中',
			icon: 'loading'
		});
		this.setData({
			screenList: null
		});
		this.switchStateScreen(e.detail.index, 1);
		this.setData({
			activeIndex: e.detail.index
		});
	},

	switchStateScreenCallBack: function(pageNum) {
		this.switchStateScreen(this.data.activeIndex, pageNum)
	},
	switchStateScreen: function(activeIndex, pageNum) {
		var pageSize = 100;
		if (activeIndex == 0) { //全部

			this.GetAllPage(pageNum, pageSize);

		} else if (activeIndex == 1) { //发布

			this.GetStatePage(pageNum, pageSize, 1);

		} else if (activeIndex == 2) { //进行中

			this.GetStatePage(pageNum, pageSize, 5);

		} else if (activeIndex == 3) { //暂停

			this.GetStatePage(pageNum, pageSize, 4);
		} else if (activeIndex == 4) { //完成

			this.GetStatePage(pageNum, pageSize, 3);
		}
	},
	
	onClick(e) {
		console.log("onclick", e.detail);
		if (e.detail.index == 0) {
			wx.navigateTo({
				url: '../../screens/add/add',
			})
		} else if (e.detail.index == 1) {
			wx.navigateTo({
				url: '../../accounts/list/list',
			})
		} else if (e.detail.index == 2) {
			wx.navigateTo({
				url: '../../screens/stat/stat',
			})
		}
	},


	showTabBarItem: function(mobilerole, roleIds, attrName) {
		let that = this;
		if (util.haveRole(mobilerole, roleIds)) {
			if (typeof that.getTabBar === 'function' &&
				that.getTabBar()) {
				that.getTabBar().setData({
					[attrName]: true
				})
			}
		} else {
			if (typeof that.getTabBar === 'function' &&
				that.getTabBar()) {
				that.getTabBar().setData({
					[attrName]: false
				})
			}
		}
	},
	//权限控制
	onTabItemTap: function(item) {
		let userInfo = globalData.userInfo;
		let mobilerole = userInfo.mobilerole_systemuser;
		var roleIds = [1, 2, 3];
		return util.checkRole(mobilerole, roleIds, function() {

			wx.switchTab({
				url: '../../tasks/list/list',
				success: function() {
					wx.showToast({
						title: '权限不足',
						duration: 2500
					})
				}
			});
		});
	},
});
